package cz.muni.fi.pa165.machineryrental.rest;

import cz.muni.fi.pa165.machineryrental.dto.CustomerDTO;
import cz.muni.fi.pa165.machineryrental.dto.CustomerDTOREST;
import cz.muni.fi.pa165.machineryrental.service.CustomerService;
import java.io.UnsupportedEncodingException;
import static java.net.URLDecoder.decode;
import java.util.List;
import javassist.tools.rmi.ObjectNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 *
 * @author Marti_000
 */
@Controller
@RequestMapping(value = "/customerRest")
public class CustomerRestController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public List<CustomerDTO> findAll() {
        return customerService.findAll();
    }

    @RequestMapping(value = "/findByLastName/{lastName}", method = RequestMethod.GET)
    @ResponseBody
    public List<CustomerDTO> findByLastName(@PathVariable(value = "lastName") String lastName) {
        return customerService.findByLastName(lastName);
    }

    @RequestMapping(value = "/findByEmail/{email}", method = RequestMethod.GET)
    @ResponseBody
    public CustomerDTO findByEmail(@PathVariable(value = "email") String email) throws UnsupportedEncodingException {
        return customerService.findByEmail(email);
    }

    @RequestMapping(value = "/findByCompanyNumber/{companyNumber}", method = RequestMethod.GET)
    @ResponseBody
    public CustomerDTO findByCompanyNumber(@PathVariable(value = "companyNumber") String companyNumber) {
        return customerService.findByCompanyNumber(companyNumber);
    }

    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CustomerDTO getById(@PathVariable(value = "id") long id) {
        return customerService.getById(id);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(value = HttpStatus.CREATED)
    public void create(@RequestBody CustomerDTOREST customer) {
        CustomerDTO dto = convert(customer);
        customerService.create(dto);
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    public void update(@PathVariable(value = "id") long id, @RequestBody CustomerDTOREST customer) {
        CustomerDTO dto = convert(customer);
        CustomerDTO customerDTO = customerService.getById(id);
        customerDTO.setCity(dto.getCity());
        customerDTO.setCompanyNumber(dto.getCompanyNumber());
        customerDTO.setCountry(dto.getCountry());
        customerDTO.setEmail(dto.getEmail());
        customerDTO.setFirstName(dto.getFirstName());
        customerDTO.setLastName(dto.getLastName());
        customerDTO.setPhone(dto.getPhone());
        customerDTO.setStreet(dto.getStreet());
        customerDTO.setVatNumber(dto.getVatNumber());
        customerDTO.setZipCode(dto.getZipCode());

        customerService.update(customerDTO);
    }
    
    @RequestMapping(value = "/isEmailUnique/{email}", method = RequestMethod.GET)
    @ResponseBody
    public boolean isEmailUnique(@PathVariable(value = "email") String email) {
        return this.customerService.isEmailUnique(email);
    }
    
    @RequestMapping(value = "/isVATUnique/{vat}", method = RequestMethod.GET)
    @ResponseBody
    public boolean isVATUnique(@PathVariable(value = "vat") String vat) {
        return this.customerService.isVATNumberUnique(vat);
    }
    
    @RequestMapping(value = "/isCompanyNumberUnique/{companynumber}", method = RequestMethod.GET)
    @ResponseBody
    public boolean isCompanyNumberUnique(@PathVariable(value = "companynumber") String companynumber) {
        return this.customerService.isCompanyNumberUnique(companynumber);
    }

    @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    public void remove(@PathVariable(value = "id") long id) throws ObjectNotFoundException {
        CustomerDTO customer = customerService.getById(id);

        if (customer == null) {
            throw new ObjectNotFoundException("Customer not found.");
        }

        customerService.remove(customer);
    }

    private CustomerDTO convert(CustomerDTOREST customer) {
        CustomerDTO dto = new CustomerDTO();
        dto.setCity(customer.getCity());
        dto.setCompanyNumber(customer.getCompanyNumber());
        dto.setCountry(customer.getCountry());
        dto.setCustomerType(customer.getCustomerType());
        dto.setEmail(customer.getEmail());
        dto.setFirstName(customer.getFirstName());
        dto.setId(customer.getId());
        dto.setLastName(customer.getLastName());
        dto.setPhone(customer.getPhone());
        dto.setRentals(customer.getRentals());
        dto.setStreet(customer.getStreet());
        dto.setVatNumber(customer.getVatNumber());
        dto.setZipCode(customer.getZipCode());
        return dto;
    }
}
